package com.example.tongue.main.Register;



import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.example.tongue.R;
import com.example.tongue.main.Register.dao.Constant;
import com.example.tongue.main.Register.entity.User;


import java.io.IOException;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class Register extends AppCompatActivity  {

    private RadioGroup sex;
    private RadioButton man, woman;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

//        man = findViewById(R.id.man);
//        woman = findViewById(R.id.woman);
        sex= findViewById(R.id.sex);



        Button register = findViewById(R.id.register);

        register.setOnClickListener(
                v->
                {
                    new Thread(() -> {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        String name = ((EditText) findViewById(R.id.name)).getText().toString();
                        String password = ((EditText) findViewById(R.id.password)).getText().toString();
                        String age = ((EditText) findViewById(R.id.age)).getText().toString();
                        String email = ((EditText) findViewById(R.id.email)).getText().toString();
                        String code="abcde";
                        //获取单选框的值
                        int sexid=sex.getCheckedRadioButtonId();
                        RadioButton sexbtn= findViewById(sexid);
                        String sexfin=sexbtn.getText().toString();


                        FormBody formBody = new FormBody.Builder()
                                .add("username", name)
                                .add("password", password)
                                .add("sex", sexfin)
                                .add("age", age)
                                .add("email",email)
                                .add("code",code)
                                .build();
                        Request request = new Request.Builder()
                                .url(Constant.register)
                                .post(formBody)
                                .build();
                        System.out.println("名字:" + name + "密码:" + password + "年纪:" + age + "性别:" + sexfin+"邮箱"+email);
                        try (Response response = okHttpClient.newCall(request).execute()) {
                            Looper.prepare();
//                           Boolean B=Boolean.parseBoolean(response.body().string());
//                            if (B) {
                                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
//                            } else {
//                                Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
//                            }
                            Looper.loop();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }).start();
                }
        );
        Button login = findViewById(R.id.login);
        login.setOnClickListener(
                v ->
                {
                    new Thread(() -> {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        String name = ((EditText) findViewById(R.id.name)).getText().toString();
                        String password = ((EditText) findViewById(R.id.password)).getText().toString();
                        FormBody formBody = new FormBody.Builder()
                                .add("name", name)
                                .add("password", password)
                                .build();
                        Request request = new Request.Builder()
                                .url(Constant.Login)
                                .post(formBody)
                                .build();
                        System.out.println("名字" + name + "密码" + password);
                        try (Response response = okHttpClient.newCall(request).execute()) {
                            List<User> users = JSONArray.parseArray(response.body().string(), User.class);
                            Looper.prepare();
                            if (users.size() == 0) {
                                Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                            }
                            Looper.loop();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }).start();
                }
        );


    }


}


